var businessTable;
$(function() {
	refresh('');
})
function searchname() {
	var a = $('#businessName').val()
	refresh(a);
}
// Table表格数据源
function refresh(aa) {
	businessTable = $('#business_table').datagrid(BasicData.createDatagrid({
		url : rootpath + '/hhBusiness/list.do?name=' + aa,
		checkOnSelect : true,
		singleSelect : true,
		selectOnCheck : true,
		frozenColumns : [ [ { field : 'ck', checkbox : false, hidden:true },
			{ title : 'businessId', field : 'businessId', hidden : true } ] ],
		columns : [ [ { field : 'name', title : '商家名称', width : '97%', sortable : true, align : 'center' },
		{ field : 'phone', title : '电话', width : '12%', hidden : true },
		{ field : 'status', title : '是否禁用', hidden : true ,formatter:function(value,row,index){
			if(value==0){
				return "否";
			}else{
				return "是";
			}
		   }}, 
		{ field : 'addTime', title : '创建时间', hidden : true,formatter:function(value,row,index){
        	return timestampToTime(value);
		   }
		} ] ],
		onDblClickRow : function(rowIndex, rowData) {
		},
		onClickRow : function(index, row) {
			$("#user_table").datagrid({
				url : rootpath + '/hhBusinessUser/list.do?businessId=' + row.businessId
			});
			//$('#dining_table').datagrid('options').queryParams = {sort:'RAMADHIN',order:'asc'};
			$("#dining_table").datagrid({
				url : rootpath + '/hhDining/list.do?businessId=' + row.businessId
			});
			  
			$("#shopmenu_table").datagrid({
				url : rootpath + '/hhFoodMenu/list.do?businessId=' +row.businessId
			});
			$("#pay_table").datagrid({
				url : rootpath + '/hhPayConfig/list.do?businessId=' +row.businessId
			});
			$("#serviceConfig_table").datagrid({
				url :  rootpath + '/hhServiceConfig/list.do?businessId=' +row.businessId
			});
			$('#food_table').datagrid('loadData', { total: 0, rows: [] });  
		}
	}));
}

var searchParams;
var userTable;
$(function() {
	// 添加按钮事件
	$('#adduser').on('click', function() {
		adduser();
	});
	// 修改按钮事件
	$('#edituser').on('click', function() {
		edituser();
	});
	// 删除按钮事件
	$('#deluser').on('click', function() {
		deluser();
	});
	// 查询按钮事件
	$('#searchuser').on('click', function() {
		searchuser();
	});
});
// Table表格数据源
userTable = $('#user_table').datagrid(BasicData.createDatagrid({
	autoWidth : true,
	url : '',
	checkOnSelect : true,
	singleSelect : true,
	selectOnCheck : true,
	frozenColumns : [ [ { 	field : 'ck', checkbox : false, hidden:true
	}, { title : 'businessUserId', field : 'businessUserId', hidden : true
	} ] ],
	columns : [ [ {field : 'businessName',title : '商家',width : '10%',sortable : true,align : 'center'}, 
		{field : 'name',title : '姓名',width : '9%',sortable : true,align : 'center'}, 
		{field : 'phone',title : '手机',width : '10%',sortable : true,align : 'center'}, 
		{field : 'role',title : '角色编号',width : '10%',sortable : true,align : 'center'},
		{field : 'loginTime',title : '最后登录时间',width : '10%',sortable : true,align : 'center'}, 
		{field : 'addTime',title : '创建时间',width : '10%',sortable : true,align : 'center',
		formatter:function(value,row,index){
        	return timestampToTime(value);
		   }
	}  ] ],
	onDblClickRow : function(rowIndex, rowData) {

	},
	onClickRow : function(index, row) {

	}
}));
// 打开查询窗体
function searchuser() {
	layer.open({
		title : '查询条件',
		type : 2,
		btn : [ '查询', '重置' ],
		yes : function(index, layero) {
			var iframeWin = window[layero.find('iframe')[0]['name']];
			searchParams = iframeWin.$("#searchForm").serialize();
			var newparam = iframeWin.$("#searchForm").serializeJSON();
			iframeWin.parentIframeName = window.name;
			$.extend(newparam, userTable.queryParams);
			var url = $('#user_table').datagrid('options').url;
			if (url == '') {
				$("#user_table").datagrid({
					url : rootpath + '/userbusiness/list.do',
					queryParams : {
						'PHONE' : +newparam.PHONE.trim()
					}
				});
			} else {
				$('#user_table').datagrid('load', newparam);
			}
			layer.close(index);
		},
		btn2 : function(index, layero) {
			var iframeWin = window[layero.find('iframe')[0]['name']];
			iframeWin.parentIframeName = window.name;
			iframeWin.$("#searchForm").form("clear");
			return false;
		},
		area : [ '80%', '80%' ],
		content : rootpath + '/hhBusinessUser/toSearch.do?' + searchParams
	});
}
// 添加打开窗体
function adduser() {
	var node = businessTable.datagrid('getSelected');
	if (node) {
		layer.open({
			title : '添加',
			type : 2,
			btn : [ '保存', '关闭' ],
			yes : function(index, layero) {
				var iframeWin = window[layero.find('iframe')[0]['name']];
				iframeWin.parentIframeName = window.name;
				iframeWin.$("#dataForm").submit();
			},
			btn2 : function(index, layero) {
			},
			area : [ '80%', '80%' ],
			content : rootpath + '/hhBusinessUser/toEdit.do?businessId=' + node.businessId
		});
	} else {
		layer.msg('请选择一条商家！', {
			time : 1000
		})
	}
}
// 编辑打开窗体
function edituser() {
	var node = userTable.datagrid('getSelected');
	if (node) {
		layer.open({
			title : '信息',
			type : 2,
			btn : [ '保存', '关闭' ],
			yes : function(index, layero) {
				var iframeWin = window[layero.find('iframe')[0]['name']];
				iframeWin.parentIframeName = window.name;
				iframeWin.$("#dataForm").submit();
			},
			btn2 : function(index, layero) {
			},
			area : [ '80%', '80%' ],
			content : rootpath + '/hhBusinessUser/toEdit.do?businessUserId=' + node.businessUserId
		});
	} else {
		layer.msg('请选择一条数据！', {
			time : 1000
		})
	}
}
// 禁用
function deluser() {
	var node = userTable.datagrid('getSelected');
	
	if (node) {
		layer.confirm('您确定要删除'+node.phone+'吗？', {
			btn : [ '确定', '取消' ]
		// 按钮
		}, function() {
			//$.uitools.showLoading();// 打开处理中遮罩层
			$.ajax({
				cache : true,
				type : "POST",
				url : rootpath + '/hhBusinessUser/delete.do',
				data : {
					businessUserId : node.businessUserId
				},
				async : false,
				success : function(data) {
					if (data.code == 0) {
						layer.msg('操作成功！', {
							icon : 1,
							time : 1000
						}, function() {
							// 刷新列表
							userTable.datagrid('reload');
						})
					} else if (data.code > 0) {
						layer.msg('操作失败，账号异常！', {
							icon : 4,
							time : 1000
						}, function() {
							// 刷新列表
							userTable.datagrid('reload');
						})
					}
				}
			});
		}, function() {
		});
	} else {
		layer.msg('请选择一条数据！', {
			time : 1000
		})
	}
}